package com.yc.sole.system.repository;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.sole.common.enums.CommonState;
import com.yc.sole.system.dto.DeptDto;
import com.yc.sole.system.entity.Dept;
import com.yc.sole.system.mapper.DeptMapper;
import com.yc.sole.system.request.DeptReq;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 服务接口实现
 *
 * @author yizuomin
 * @date 2023-03-26
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class DeptRepository extends ServiceImpl<DeptMapper, Dept> {

    /**
     * 新增
     *
     * @param deptDto
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveDept(DeptDto deptDto) {
        Dept entity = new Dept();
        BeanUtil.copyProperties(deptDto, entity);
        this.save(entity);
    }

    /**
     * 通过Id修改
     *
     * @param deptDto
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateDeptById(DeptDto deptDto) {
        Dept entity = new Dept();
        BeanUtil.copyProperties(deptDto, entity);
        this.updateById(entity);
    }

    /**
     * 通过id删除
     *
     * @param ids
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteDeptByIds(Collection<Long> ids) {
        this.removeBatchByIds(ids);
    }

    /**
     * 根据id查询
     *
     * @param deptIds
     * @return
     */
    public List<DeptDto> selectDeptByIds(Set<Long> deptIds) {
        if (CollUtil.isEmpty(deptIds)) {
            return Collections.emptyList();
        }
        return BeanUtil.copyToList(this.listByIds(deptIds), DeptDto.class);
    }

    /**
     * 根据id查询
     *
     * @param deptId
     * @return
     */
    public DeptDto selectDeptById(Long deptId) {
        return BeanUtil.copyProperties(this.getById(deptId), DeptDto.class);
    }

    /**
     * 查询所有
     *
     * @return
     */
    public List<DeptDto> selectAll() {
        LambdaQueryWrapper<Dept> wrapper = Wrappers.<Dept>lambdaQuery().eq(Dept::getState, CommonState.ENABLE);
        return BeanUtil.copyToList(this.list(wrapper), DeptDto.class);
    }

    /**
     * 查询所有
     *
     * @return
     */
    public List<DeptDto> selectDeptTree(DeptReq deptReq) {
        LambdaQueryWrapper<Dept> queryWrapper = Wrappers.<Dept>lambdaQuery()
                .like(CharSequenceUtil.isNotBlank(deptReq.getDeptName()), Dept::getDeptName, deptReq.getDeptName())
                .eq(Objects.nonNull(deptReq.getState()), Dept::getState, deptReq.getState());
        return BeanUtil.copyToList(this.list(queryWrapper), DeptDto.class);
    }
}
